' This file is part of the SunDLL.  The SunDLL is free software: you can
' redistribute it and/or modify it under the terms of the GNU General Public
' License as published by the Free Software Foundation, version 2.
'
' This program is distributed in the hope that it will be useful, but WITHOUT
' ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
' FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
' details.
'
' You should have received a copy of the GNU General Public License along with
' this program; if not, write to the Free Software Foundation, Inc., 51
' Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
'
' Copyright David R. Pratten
'REGISTRY.INC
'General Purpose Registry Access
'Uses: win32api.inc
'Functions:
' DelRegValue(lpKey As Long,ByVal cMainkey As String, ByVal Key As String) As Long
' GetRegValue(lpKey As Long,ByVal cMainkey As String, ByVal Key As String) As String
' SetRegValue(lpKey As Long,ByVal cMainkey As String, ByVal Key As String, ByVal Setting As String) As Long
' DelRegKey(lpKey As Long, Key As String) Export As Long
Function DelRegValue(lpKey As Long,ByVal cMainkey As String, ByVal Key As String) As Long
On Error Resume Next
Local RetCode As Long
Local hKey As Long
Dim acMainkey As Asciiz * 300
     acMainkey = cMainkey
     RetCode = RegOpenKeyEx(lpKey, acMainkey, 0&, %KEY_ALL_ACCESS, hKey)
     If RetCode = %ERROR_SUCCESS Then
       If Key$ = "*" Then Key$ = Chr$(0,0)
       RetCode = RegDeleteValue(hKey, ByVal StrPtr(Key$))
     End If
     RegCloseKey hKey
     Function = RetCode
End Function

Function GetRegValue(lpKey As Long,ByVal cMainkey As String, ByVal Key As String) As String
On Error Resume Next
Dim RetCode As Long
Dim hKey As Long
Dim KeyNameA As Asciiz * 256
Local zTmp As Asciiz * 2048
Dim acMainKey As Asciiz * 300
Dim szdat&, cbData&, KeyType&
Local ZZZ As String
     acMainKey = cMainKey
     RetCode = RegOpenKeyEx(lpKey, acMainkey, 0&, %KEY_ALL_ACCESS, hKey)
     If RetCode = %ERROR_SUCCESS Then
       If Key$ = "*" Then Key$ = Chr$(0,0)
       szdat&=256
       Dim zbuffer As Asciiz*256
       KeyNameA = Key
       cbData& = SizeOf(zTmp)
       RetCode = RegQueryValueEx(ByVal hKey, KeyNameA, ByVal 0, KeyType&, zTmp, cbData&)
       ZZZ = zTmp
       Function = ZZZ
       Exit Function
     End If
     Function = ""
End Function

Function SetRegValue(lpKey As Long,ByVal cMainkey As String, ByVal Key As String, ByVal Setting As String) As Long
On Error Resume Next
Local hKey   As Long
Local Result As Long
Local zText  As Asciiz * 2048
If Key$ = "*" Then Key$ = Chr$(0,0)
If RegCreateKeyEx(lpKey, cMainKey + Chr$(0),0, "", %REG_OPTION_NON_VOLATILE, _
                   %KEY_ALL_ACCESS, ByVal %NULL, hKey, Result) <> %ERROR_SUCCESS Then
    Function = 0
    Exit Function
End If
zText = Setting
If Len(Setting) Then
    RegSetValueEx hKey, Key+Chr$(0), 0, %REG_SZ, zText, Len(Setting)+1
Else
    RegSetValueEx hKey, Key+Chr$(0), 0, %REG_SZ, zText, 1
End If
RegCloseKey hKey
Function = 0
End Function

Function DelRegKey(lpKey As Long, Key As String) Export As Long
On Error Resume Next
Local zStrKey As Asciiz * 255, hKey As Long, Result As Long
zStrKey = Key
If RegOpenKeyEx(ByVal lpKey,zStrKey,0,  %KEY_ALL_ACCESS,  hKey) <> %ERROR_SUCCESS Then  Exit Function
Result = RegDeleteKey(lpKey, zStrKey)
RegCloseKey hKey
Function = Result
End Function

